var restify = require('restify')
var mysql = require('mysql')
var dateHelper = require('./utils/date.js')

var server = restify.createServer({
    name: 'cch',
    version: '1.0.0'
});

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
restify.CORS.ALLOW_HEADERS.push('authorization');

server.use(restify.CORS({
    origins: ['http://wechat.juzhen.net', 'http://wx.juzhen.net', '*'], // defaults to ['*']
    headers: ['Authorization'] // sets expose-headers
}));

server.use(restify.throttle({
    burst: 1000,
    rate: 500,
    ip: true
}));

var dbPool = mysql.createPool({
    connectionLimit: 50,
    host: 'localhost',
    user: 'xxf',
    password: 'xiexiaofei',
    database: 'production'
});

// global object
global.dbPool = dbPool;
global.server = server;

var path = require('path');
var dir = require('./utils/dir.js')

dir.requireDirRecursive(path.format({ dir: __dirname, base: 'src' }))

server.listen(61339, function () {
    console.log(dateHelper.now() + '  长城汇项目已启动...')
});



// var redis = require("redis"),
//     client = redis.createClient();

// // if you'd like to select database 3, instead of 0 (default), call 
// // client.select(3, function() { /* ... */ }); 

// client.on("error", function (err) {
//     console.log("Error " + err);
// });

// client.set("user", "aaaaaaaaa")

// client.get('user', function (err, reply) {
//     // reply is null when the key is missing 
//     console.log(reply);
// })